package homework;
//作业标题(2310)
//求环的入口点
//
//作业内容
//求环的入口点
//https://leetcode.cn/problems/linked-list-cycle-ii/description/
public class h2310 {
    class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }
    public ListNode detectCycle(ListNode head) {
        if(head == null || head.next == null) return null;

        ListNode fast = head;
        ListNode last = head;
        while(fast != null && fast.next != null)
        {
            fast = fast.next.next;
            last = last.next;
            if(fast == last)
            {
                break;
            }
        }
        if(fast == null || fast.next == null)
        {
            return null;
        }

        fast = head;
        while(fast != last)
        {
            fast = fast.next;
            last = last.next;
        }
        return fast;
    }
}
